import UIManager from "../../engine/UIManager";
import ViewBase from "../../engine/ViewBase";
import GameController from "../../GameController";
import { SocketClient } from "../../net/SocketClient";
import { RoomJinbiConfig } from "../config/RoomJinbiConfig";
import { ViewConfig } from "../config/ViewConfig";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewZiyouRoom extends ViewBase {

    private changContainer: cc.Node

    onEnable(): void {
        super.onEnable()
        this.changContainer = this.findChild(this.node, "scrollView/view/content")
        let btnBack: cc.Node = this.findChild(this.node, "btnBack")
        UIHelper.onAddEvent(btnBack, () => {
            GameController.instance.gotoHall(true)
        })

        SocketClient.req_ziyouchang_roomconfig((dataList: Array<object>) => {
            RoomJinbiConfig.json2_ziyouchang_list(dataList)
            this.refresh()
        })
    }


    private refresh() {
        for (let i = 0; i < 3; i++) {
            let config: RoomJinbiConfig = RoomJinbiConfig.ziyouchang_list[i]
            let chang: cc.Node = this.findChild(this.changContainer, "chang" + i)
            let txtMinScore: cc.Label = this.getChild(chang, "txtMinScore", cc.Label)
            let txtCost: cc.Label = this.getChild(chang, "txtCost", cc.Label)
            let txtOnline: cc.Label = this.getChild(chang, "txtOnline", cc.Label)
            if (config) {
                txtMinScore.string = "" + config.enterMin
                txtCost.string = "" + config.serve
                txtOnline.string = "" + config.online
                UIHelper.onAddEvent(chang, () => {
                    UIManager.instance.showView(ViewConfig.viewConfig.ViewZiyouDesk, ()=>{
                        this.closeView()
                    }, config)
                }, cc.Button.Transition.NONE)
            }
        }
    }

}
